package com.kool.kreport.common.util;

import com.kool.kreport.common.constant.ErrorCode;
import com.kool.kreport.common.exception.AppException;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @author luyu
 */
@Slf4j
public class CmdUtil {
    /**
     * 执行cmd命令
     *
     * @param cmd
     */
    public static void exec(String[] cmd) {
        Runtime runtime = Runtime.getRuntime();
        try {
            Process process = runtime.exec(cmd);
            new LogThread(process.getInputStream(), "./cmd.log").start();
            new LogThread(process.getErrorStream(), "./cmd.log").start();
            process.waitFor();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new AppException(ErrorCode.SYSTEM_ERROR, "执行cmd命令失败！");
        } catch (InterruptedException e) {
            log.error(e.getMessage(), e);
            throw new AppException(ErrorCode.SYSTEM_ERROR, "执行cmd命令失败！");
        }
    }

}
